package top.yjp.jtf.plugin.deprecated;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.table.JBTable;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelEditor2 extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        VirtualFile[] files = e.getData(com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE_ARRAY);

        if (files != null && files.length > 0) {
            for (VirtualFile file : files) {
                if (file.getName().endsWith(".xlsx")) {
                    openExcelFile(file);
                    break;
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "请选择一个 Excel 文件！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void openExcelFile(VirtualFile virtualFile) {
        File file = new File(virtualFile.getPath());

        // 读取 Excel 数据
        List<Map<Integer, String>> data = EasyExcel.read(file).excelType(ExcelTypeEnum.XLSX).sheet().doReadSync();

        if (data.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Excel 文件没有数据！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 获取列数
        int columnCount = data.get(0).size();

        // 创建列名
        String[] columnNames = new String[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnNames[i] = data.get(0).get(i);//"Column " + (i + 1); // 使用适当的列名
        }

        // 创建表格模型
        DefaultTableModel model = new DefaultTableModel(columnNames, data.size());

        for (int i = 0; i < data.size(); i++) {
            Map<Integer, String> row = data.get(i);
            final int rowIndex = i;
            row.forEach((k, v) -> {
                if (k < columnCount) { // 确保索引有效
                    model.setValueAt(v, rowIndex, k);
                }
            });
        }

        // 创建 JBTable 并设置模型
        JBTable table = new JBTable(model);

        // 创建滚动面板
        JBScrollPane scrollPane = new JBScrollPane(table);

        // 创建对话框
        JOptionPane.showMessageDialog(null, scrollPane, "Excel 编辑器", JOptionPane.PLAIN_MESSAGE);

        // 保存修改后的数据
        int option = JOptionPane.showConfirmDialog(null, "是否保存修改？", "保存", JOptionPane.YES_NO_OPTION);
        if (option == JOptionPane.YES_OPTION) {
            saveExcelFile(file, table);
        }
    }


    private void saveExcelFile(File file, JTable table) {
        // 获取表格数据
        int rowCount = table.getRowCount();
        int columnCount = table.getColumnCount();
        List<List<String>> data = new ArrayList<>();

        for (int i = 0; i < rowCount; i++) {
            List<String> row = new ArrayList<>();
            for (int j = 0; j < columnCount; j++) {
                row.add((String) table.getValueAt(i, j));
            }
            data.add(row);
        }

        // 写入 Excel
        EasyExcel.write(file).head(List.class).sheet("Sheet1").doWrite(data);
        JOptionPane.showMessageDialog(null, "文件已保存！", "保存成功", JOptionPane.INFORMATION_MESSAGE);
    }
}
